<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="灯杆名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lightName">
              <a-input v-model="model.lightName" placeholder="请输入灯杆名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="灯杆编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lightCode">
              <a-input v-model="model.lightCode" placeholder="请输入灯杆编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gwlongitude">
              <a-input v-model="model.gwlongitude" placeholder="请输入经度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gwlatitude">
              <a-row :gutter="24">
                <a-col :span="16">
                <a-input v-model="model.gwlatitude" placeholder="请输入纬度"  ></a-input>
                </a-col>
                <a-col :span="8">
                <a-button @click="openSetLocation()" type="primary">选择</a-button>
                      <set-location ref="setLocation" @ok="setLocationOk" ></set-location>
                </a-col>
              </a-row>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="云盒zigbee 中心地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="boxId">
              <a-input v-model="model.boxId" placeholder="请输入云盒zigbee 中心地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="云盒类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="boxType">
              <j-dict-select-tag type="list" v-model="model.boxType" dictCode="boxType" placeholder="请选择云盒类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="云盒mac地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="boxMac">
              <a-input v-model="model.boxMac" placeholder="请输入云盒mac地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="在线状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="olStatus">
              <j-switch v-model="model.olStatus"  ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="device_status" placeholder="请选择设备状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="false">
            <a-form-model-item label="所属区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaId">
              <j-dict-select-tag type="list" v-model="model.areaId" dictCode="areaId" placeholder="请选择所属区域" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="false">
            <a-form-model-item label="所属分组id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupId">
              <j-popup
                v-model="model.groupId"
                field="groupId"
                org-fields="id"
                dest-fields="groupName"
                code="tb_group"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24"   v-if="false">
            <a-form-model-item label="所属项目id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <j-popup
                v-model="model.projectId"
                field="projectId"
                org-fields="id"
                dest-fields="projectName"
                code="tb_project"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SetLocation from './SetLocation'
  export default {
    name: 'LightPoleForm',
    components: {
      SetLocation
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          gwlongitude:'',
          gwlatitude:'',
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           lightName: [
              { required: true, message: '请输入灯杆名称!'},
           ],
           lightCode: [
              { required: true, message: '请输入灯杆编号!'},
           ],
        },
        url: {
          add: "/corp/lightPole/add",
          edit: "/corp/lightPole/edit",
          queryById: "/corp/lightPole/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      openSetLocation(){
        this.$refs.setLocation.openDialog(this.model.gwlongitude,this.model.gwlatitude);
      },
      setLocationOk(longitude,latitude){
        this.model.gwlongitude=longitude;
        this.model.gwlatitude=latitude;
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>